package com.spx.core.wizards;

import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.spx.core.CoreActivator;
import com.spx.core.types.Activity;
import com.spx.core.types.Role;
import com.spx.core.types.User;
import com.spx.kernel.types.ModelType;
import com.spx.kernel.ui.widgets.DoubleListSelector;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (mpe).
 */

public class NewRolePage extends ModelWizardPage<Role>
{
	private final Role role = new Role();
	private Text nameText;
	private DoubleListSelector<Activity> selector;

	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public NewRolePage()
	{
		super("wizardPage");
		setTitle("New Role Basic Details");
		setDescription("This wizard creates a Role.");

	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent)
	{

		final GridLayout parentLayout = new GridLayout();
		parentLayout.numColumns = 1;
		parent.setLayout(parentLayout);

		final Composite nameComposite = new Composite(parent, SWT.NONE);
		final Group roleComposite = new Group(parent, SWT.NONE);
		roleComposite.setText("Select  Roles");
		roleComposite.setLayout(parentLayout);

		final GridLayout layout = new GridLayout();
		nameComposite.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		final Label label = new Label(nameComposite, SWT.NULL);
		label.setText("&Name:");

		nameText = new Text(nameComposite, SWT.BORDER | SWT.SINGLE);
		final GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);

		selector = new DoubleListSelector<Activity>(roleComposite, SWT.NONE, getAllActions(), role.getActions());

		initialize();
		dialogChanged();
		setControl(selector);
	}

	private Set<Activity> getAllActions()
	{
		final Set<Activity> result = CoreActivator.getDefault().getSecurityService().fetchAllActions(0, Integer.MAX_VALUE);
		return result;
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */

	private void initialize()
	{

	}

	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged()
	{

	}

	private void updateStatus(String message)
	{
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	@Override
	public void setProperties(Role role)
	{
		role.setName(StringUtils.trim(nameText.getText()));

		role.getActions().addAll(selector.getChosen());

	}

	@Override
	protected void createPageControl(Composite parent)
	{
		// TODO Auto-generated method stub

	}
}

class _ModelLabelProvider extends LabelProvider implements ITableLabelProvider
{
	public String getColumnText(Object obj, int index)
	{
		return getText(obj);
	}

	@Override
	public String getText(Object object)
	{
		final String result = ((ModelType) object).getName();
		return result;

	}

	public Image getColumnImage(Object obj, int index)
	{
		return getImage(obj);
	}

	@Override
	public Image getImage(Object object)
	{

		Image image = null;
		if (object instanceof User)
		{
			image = CoreActivator.getImageDescriptor("icons/user_16.gif").createImage();
		}

		return image;
	}
}

class NameSorter extends ViewerSorter
{
}
